﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DevExpress.Xpf.Core;
using ThanNhanTao.Server;
using ThanNhanTao.Server.Entities;
using ThanNhanTao.Server.BLL;
using DevExpress.XtraEditors.DXErrorProvider;
using ThanNhanTao.Report;
using ThanNhanTao.CommonControl;


namespace ThanNhanTao
{
    /// <summary>
    /// Interaction logic for frmPhieuThongTinHoSoCongChucVienChuc.xaml
    /// </summary>
    public partial class frmPhieuThongTinHoSoCongChucVienChuc : DXWindow
    {
        #region Fields

        private PhieuBenhNhanBll phieuBenhNhanBll;
        private HoSoNhanSuEntities hoSoHienTai;
        private HoSoNhanSuBLL hoSoNhanSuBLL;
        private List<Dm_DanToc> listDanToc;

        #endregion

        #region Properties

        public HoSoNhanSuEntities HoSoHienTai
        {
            get
            {
                if (hoSoHienTai == null)
                    return new HoSoNhanSuEntities();
                return hoSoHienTai;
            }
            set
            {
                hoSoHienTai = value;
            }
        }

        public bool LaThemMoi
        {
            get
            {
                return HoSoHienTai.IdHoSoNhanSu == 0;
            }
        }

        #endregion

        public frmPhieuThongTinHoSoCongChucVienChuc()
        {
            InitializeComponent();
            try
            {
                phieuBenhNhanBll = new PhieuBenhNhanBll();
                hoSoNhanSuBLL = new HoSoNhanSuBLL();
                ucDanhSach.formContain = this;
                LoadForm();
                ThemMoi();
            }
            catch (Exception)
            {
                DXMessageBox.Show("Thiết lập thông tin không thành công", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        public void BindDanToc()
        {
            listDanToc = phieuBenhNhanBll.GetListDanToc();
            cboDanToc.ItemsSource = listDanToc;
            cboDanToc.DisplayMember = "TenDanToc";
        }

        public void LoadForm()
        {
            ucDanhSach.BindHoSuNhanSu();
            BindDanToc();
        }

        #region Method

        public void SetEnableButton(bool buttonThemMoi, bool buttonLuu)
        {
            btnLuu.IsEnabled = buttonLuu;
            btnThemMoi.IsEnabled = buttonThemMoi;
        }

        public void ThemMoi()
        {
            txtHo.CausesValidation = false;
            txtTen.CausesValidation = false;
            txtNamSinh.CausesValidation = false;
            txtHo.Clear();
            txtTen.Clear();
            rdoNam.IsChecked = true;
            txtNgaySinh.Clear();
            txtThangSinh.Clear();
            txtNamSinh.Clear();
            cboDanToc.EditValue = null;
            txtTinhTrangHonNhan.Clear();
            txtDiaChiThuongTru.Clear();
            txtNguyenQuan.Clear();
            txtSoHieuCongChuc.Clear();
            txtDienThoaiDiDong.Clear();
            txtDiaChiEmail.Clear();
            rdoDangVienKhong.IsChecked = true;
            rdoDangVienCo.IsChecked = false;
            deKetNap.EditValue = null;
            deChinhThuc.EditValue = null;
            txtChucVuDang.Clear();
            HoSoHienTai = new HoSoNhanSuEntities();
            SetEnableButton(false, false);
            btnInPhieu.IsEnabled = false;
        }

        public void BindHoSoNhanSuHienTai(HoSoNhanSuEntities hoSo)
        {
            this.hoSoHienTai = hoSo;
            txtHo.Text = hoSoHienTai.Ho;
            txtTen.Text = hoSoHienTai.Ten;
            if (hoSoHienTai.GioiTinh)
                rdoNam.IsChecked = true;
            else
                rdoNu.IsChecked = true;
            if (hoSoHienTai.NgaySinh != null)
            {
                txtNgaySinh.Text = hoSoHienTai.NgaySinh.Value.Day.ToString();
                txtThangSinh.Text = hoSoHienTai.NgaySinh.Value.Month.ToString();
            }
            else
            {
                txtNgaySinh.Clear();
                txtThangSinh.Clear();
            }
            txtNamSinh.Text = hoSoHienTai.NamSinh.ToString();
            cboDanToc.EditValue = listDanToc.FirstOrDefault(u => u.MaDanToc == hoSoHienTai.MaDanToc);
            txtTinhTrangHonNhan.Text = hoSoHienTai.TinhTrangHonNhan;
            txtDiaChiThuongTru.Text = hoSoHienTai.DiaChiThuongTru;
            txtNguyenQuan.Text = hoSoHienTai.NguyenQuan;
            txtSoHieuCongChuc.Text = hoSoHienTai.SoHieuCongChuc;
            txtDienThoaiDiDong.Text = hoSoHienTai.DienThoaiDiDong;
            txtDiaChiEmail.Text = hoSoHienTai.DiaChiEmail;
            if (hoSoHienTai.LaDangVien)
                rdoDangVienCo.IsChecked = true;
            else
                rdoDangVienKhong.IsChecked = true;
            if (hoSoHienTai.NgayKetNapDang != null)
                deKetNap.EditValue = hoSoHienTai.NgayKetNapDang.Value;
            else
                deKetNap.EditValue = null;
            if (hoSoHienTai.NgayChinhThucVaoDang != null)
                deChinhThuc.EditValue = hoSoHienTai.NgayChinhThucVaoDang.Value;
            else
                deChinhThuc.EditValue = null;
            txtChucVuDang.Text = hoSoHienTai.ChucVuDang;
            SetEnableButton(true, false);
            btnInPhieu.IsEnabled = true;
        }

        #endregion

        #region Events

        private void btnLuu_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                txtHo.CausesValidation = true;
                txtHo.DoValidate();
                if (txtHo.HasValidationError)
                {
                    txtHo.Focus();
                    return;
                }
                txtTen.CausesValidation = true;
                txtTen.DoValidate();
                if (txtTen.HasValidationError)
                {
                    txtTen.Focus();
                    return;
                }
                txtNamSinh.CausesValidation = true;
                txtNamSinh.DoValidate();
                if (txtNamSinh.HasValidationError)
                {
                    txtNamSinh.Focus();
                    return;
                }

                DateTime ngaySinh = DateTime.Now;
                if (!string.IsNullOrEmpty(txtNgaySinh.Text) && !string.IsNullOrEmpty(txtThangSinh.Text) && !string.IsNullOrEmpty(txtNamSinh.Text))
                {
                    try
                    {
                        if (!DateTime.TryParse(new DateTime(Convert.ToInt32(txtNamSinh.Text)
                                                        , Convert.ToInt32(txtThangSinh.Text)
                                                        , Convert.ToInt32(txtNgaySinh.Text)).ToShortDateString(), out ngaySinh))
                        {
                            DXMessageBox.Show("Ngày sinh không hợp lệ", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                            txtNgaySinh.Focus();
                            return;
                        }
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        DXMessageBox.Show("Ngày sinh không hợp lệ", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                        txtNgaySinh.Focus();
                        return;
                    }
                }
                

                if (hoSoHienTai == null)
                    hoSoHienTai = new HoSoNhanSuEntities();
                Dm_DanToc danToc = cboDanToc.EditValue as Dm_DanToc;
                hoSoHienTai.Ho = txtHo.Text;
                hoSoHienTai.Ten = txtTen.Text;
                hoSoHienTai.GioiTinh = rdoNam.IsChecked == true;
                if (!string.IsNullOrEmpty(txtNgaySinh.Text) && !string.IsNullOrEmpty(txtThangSinh.Text) && !string.IsNullOrEmpty(txtNamSinh.Text))
                    hoSoHienTai.NgaySinh = ngaySinh;
                hoSoHienTai.NamSinh = Convert.ToInt32(txtNamSinh.Text);
                hoSoHienTai.Tuoi = (short)(DateTime.UtcNow.Year - Convert.ToInt32(txtNamSinh.Text));
                if (danToc != null)
                    hoSoHienTai.MaDanToc = danToc.MaDanToc; ;
                hoSoHienTai.TinhTrangHonNhan = txtTinhTrangHonNhan.Text;
                hoSoHienTai.DiaChiThuongTru = txtDiaChiThuongTru.Text;
                hoSoHienTai.NguyenQuan = txtNguyenQuan.Text;
                hoSoHienTai.SoHieuCongChuc = txtSoHieuCongChuc.Text;
                hoSoHienTai.DienThoaiDiDong = txtDienThoaiDiDong.Text;
                hoSoHienTai.DiaChiEmail = txtDiaChiEmail.Text;
                hoSoHienTai.LaDangVien = rdoDangVienCo.IsChecked == true;
                if (!string.IsNullOrEmpty(deKetNap.DisplayText))
                    hoSoHienTai.NgayKetNapDang = Convert.ToDateTime(deKetNap.EditValue);
                else
                    hoSoHienTai.NgayKetNapDang = null;
                if (!string.IsNullOrEmpty(deChinhThuc.DisplayText))
                    hoSoHienTai.NgayChinhThucVaoDang = Convert.ToDateTime(deChinhThuc.EditValue);
                else
                    hoSoHienTai.NgayChinhThucVaoDang = null;
                hoSoHienTai.ChucVuDang = txtChucVuDang.Text;
                if (LaThemMoi)
                {
                    hoSoNhanSuBLL.AddHoSoNhanSu(ref hoSoHienTai);
                    ucDanhSach.AddHoSoNhanSuToGrid(hoSoHienTai);
                    btnThemMoi.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                }
                else
                {
                    hoSoNhanSuBLL.UpdateHoSoNhanSu(hoSoHienTai);
                    ucDanhSach.UpdateHoSoNhanSuToGrid(hoSoHienTai);
                    SetEnableButton(true, false);
                }
                DXMessageBox.Show("Lưu thông tin thành công", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
                
            }
            catch (Exception)
            {
                DXMessageBox.Show("Lưu thông tin không thành công", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnInPhieu_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                HoSoNhanSuEntities hoSoInPhieu = hoSoNhanSuBLL.InPhieuThongTinHoSoCongChucVienChuc(hoSoHienTai.IdHoSoNhanSu);
                if (hoSoInPhieu == null)
                {
                    DXMessageBox.Show("Hãy chọn nhân sự cần in phiếu", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                rptPhieuThongTinHoSoCongChucVienChuc report = new rptPhieuThongTinHoSoCongChucVienChuc();
                report.BindThongTinChung(hoSoInPhieu);
                ReportCommonWindow windowReport = new ReportCommonWindow(report);
                windowReport.Title = "In hồ sơ nhân sự";
                windowReport.ShowDialog();
            }
            catch (Exception)
            {
                DXMessageBox.Show("In phiếu không thành công", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnDong_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnXoa_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                HoSoNhanSuEntities hoSoXoa = this.ucDanhSach.gridNhanSu.GetFocusedRow() as HoSoNhanSuEntities;
                if (hoSoXoa != null)
                {
                    if (DXMessageBox.Show("Bạn chắc chắn muốn xóa thông tin này?", "Chú ý", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                    {
                        hoSoNhanSuBLL.DeleteHoSoNhanSu(hoSoXoa.IdHoSoNhanSu);
                        ucDanhSach.DeleteHoSoNhanSuToGrid(hoSoXoa.IdHoSoNhanSu);
                        if (hoSoXoa.IdHoSoNhanSu == HoSoHienTai.IdHoSoNhanSu)
                            btnThemMoi.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                        DXMessageBox.Show("Xóa thông tin thành công", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                }
            }
            catch (Exception)
            {
                DXMessageBox.Show("Xóa thông tin không thành công", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnThemMoi_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ThemMoi();
            }
            catch (Exception)
            {
                DXMessageBox.Show("Thêm mới không thành công", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        #endregion

        private void cboDanToc_EditValueChanging(object sender, DevExpress.Xpf.Editors.EditValueChangingEventArgs e)
        {
            SetEnableButton(true, true);
        }

        private void rdoDangVienKhong_Checked(object sender, RoutedEventArgs e)
        {
            SetEnableButton(true, true);
            deChinhThuc.IsEnabled = false;
            deKetNap.IsEnabled = false;
            deChinhThuc.EditValue = null;
            deKetNap.EditValue = null;
        }

        private void rdoDangVienCo_Checked(object sender, RoutedEventArgs e)
        {
            SetEnableButton(true, true);
            deChinhThuc.IsEnabled = true;
            deKetNap.IsEnabled = true;
        }

        private void deKetNap_EditValueChanging(object sender, DevExpress.Xpf.Editors.EditValueChangingEventArgs e)
        {
            SetEnableButton(true, true);
        }

        private void deChinhThuc_EditValueChanging(object sender, DevExpress.Xpf.Editors.EditValueChangingEventArgs e)
        {
            SetEnableButton(true, true);
        }

        private void txtChucVuDang_EditValueChanging(object sender, DevExpress.Xpf.Editors.EditValueChangingEventArgs e)
        {
            SetEnableButton(true, true);
        }

        private void txtDiaChiEmail_EditValueChanging(object sender, DevExpress.Xpf.Editors.EditValueChangingEventArgs e)
        {
            SetEnableButton(true, true);
        }

        private void txtDienThoaiDiDong_EditValueChanging(object sender, DevExpress.Xpf.Editors.EditValueChangingEventArgs e)
        {
            SetEnableButton(true, true);
        }

        private void txtSoHieuCongChuc_EditValueChanging(object sender, DevExpress.Xpf.Editors.EditValueChangingEventArgs e)
        {
            SetEnableButton(true, true);
        }

        private void txtNguyenQuan_EditValueChanging(object sender, DevExpress.Xpf.Editors.EditValueChangingEventArgs e)
        {
            SetEnableButton(true, true);
        }

        private void txtDiaChiThuongTru_EditValueChanging(object sender, DevExpress.Xpf.Editors.EditValueChangingEventArgs e)
        {
            SetEnableButton(true, true);
        }

        private void txtTinhTrangHonNhan_EditValueChanging(object sender, DevExpress.Xpf.Editors.EditValueChangingEventArgs e)
        {
            SetEnableButton(true, true);
        }

        private void txtNgaySinh_EditValueChanging(object sender, DevExpress.Xpf.Editors.EditValueChangingEventArgs e)
        {
            SetEnableButton(true, true);
        }

        private void txtThangSinh_EditValueChanging(object sender, DevExpress.Xpf.Editors.EditValueChangingEventArgs e)
        {
            SetEnableButton(true, true);
        }

        private void rdoNam_Checked(object sender, RoutedEventArgs e)
        {
            SetEnableButton(true, true);
        }

        private void rdoNu_Checked(object sender, RoutedEventArgs e)
        {
            SetEnableButton(true, true);
        }

        private void txtTen_EditValueChanging(object sender, DevExpress.Xpf.Editors.EditValueChangingEventArgs e)
        {
            SetEnableButton(true, true);
        }

        private void txtHo_EditValueChanging(object sender, DevExpress.Xpf.Editors.EditValueChangingEventArgs e)
        {
            SetEnableButton(true, true);
        }

        private void txtNamSinh_EditValueChanging(object sender, DevExpress.Xpf.Editors.EditValueChangingEventArgs e)
        {
            SetEnableButton(true, true);
        }

        private void txtHo_Validate(object sender, DevExpress.Xpf.Editors.ValidationEventArgs e)
        {
            if (e.Value == null || string.IsNullOrEmpty(e.Value.ToString()))
            {
                e.SetError("Vui lòng nhập họ", ErrorType.Critical);
                e.IsValid = false;
            }
            else
                e.IsValid = true;
        }

        private void txtTen_Validate(object sender, DevExpress.Xpf.Editors.ValidationEventArgs e)
        {
            if (e.Value == null || string.IsNullOrEmpty(e.Value.ToString()))
            {
                e.SetError("Vui lòng nhập Tên",ErrorType.Critical);
                e.IsValid = false;
            }
            else
                e.IsValid = true;
        }

        private void txtNamSinh_Validate(object sender, DevExpress.Xpf.Editors.ValidationEventArgs e)
        {
            if (e.Value == null || string.IsNullOrEmpty(e.Value.ToString()) ||  Convert.ToInt32(e.Value) == 0)
            {
                e.SetError("Vui lòng nhập năm sinh", ErrorType.Critical);
                e.IsValid = false;
            }
            else
                e.IsValid = true;
        }
    }
}
